package com.heima.service;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;  
import org.apache.pdfbox.pdmodel.PDPageContentStream;  
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Service
public class PdfBoxService {
    public byte[] generatePdf() throws Exception {
        PDDocument document = new PDDocument();  
        PDPage page = new PDPage();  
        document.addPage(page);  
  
        PDPageContentStream contentStream = new PDPageContentStream(document, page);  
        contentStream.beginText();  
        contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);  
        contentStream.newLineAtOffset(100, 700);  
        contentStream.showText("Hello, World!");  
        contentStream.endText();  
        contentStream.close();  
  
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            document.save(outputStream);  
            return outputStream.toByteArray();  
        } catch (IOException e) {
            e.printStackTrace();  
            return null;  
        } finally {  
            document.close();  
        }  
    }  
}